/**
 * Created by Frings on 2017/5/23 parts-property.
 */

/*商品属性展示*/
function BomPartsShow(vaulue_rule, temp_id) {
    this.ValueRule = vaulue_rule;
    this.TempId = temp_id;
}

BomPartsShow.prototype = {
    getHtml: function () {
        var property_type = this.ValueRule.type;
        switch (property_type) {
            case "string":
                return this.stringHtml();
            case "number":
                return this.numberHtml();
            case "enum":
                return this.enumHtml();
            case "list":
                return this.listHtml();
            case "table":
                return this.tableHtml();
            case "file":
                return this.fileHtml();
            case "auto":
                return this.autoHtml();
        }
    },

    stringHtml: function () {
        var min_length = this.ValueRule.min_length;
        var max_length = this.ValueRule.max_length;
        var html = '';
        html += '<label class="control-label col-lg-1">最小长度：</label>';
        html += '<div class="col-lg-1"><input type="text" disabled="disabled" value="' + min_length + '" class="form-control"></div>';
        html += '<label class="control-label col-lg-1">最大长度：</label>';
        html += '<div class="col-lg-1"><input type="text" disabled="disabled" value="' + max_length + '" class="form-control"></div>';
        return html;
    },

    numberHtml: function () {
        var min_length = this.ValueRule.min;
        var max_length = this.ValueRule.max;
        var step = this.ValueRule.step;
        var is_integer = this.ValueRule.is_integer;

        var html = '';
        //html += '<label class="control-label col-lg-1">是否整型：</label>';
        if (is_integer == 1) {
            html += '<div class="col-lg-1"><input type="text" disabled="disabled" value="整型" class="form-control"></div>';
        } else {
            html += '<div class="col-lg-1"><input type="text" disabled="disabled" value="非整型" class="form-control"></div>';
        }
        html += '<label class="control-label col-lg-1">最小值：</label>';
        html += '<div class="col-lg-1"><input type="text" disabled="disabled" value="' + min_length + '" class="form-control"></div>';
        html += '<label class="control-label col-lg-1">最大值：</label>';
        html += '<div class="col-lg-1"><input type="text" disabled="disabled" value="' + max_length + '" class="form-control"></div>';
        html += '<label class="control-label col-lg-1">变化步长：</label>';
        html += '<div class="col-lg-1"><input type="text" disabled="disabled" value="' + step + '" class="form-control"></div>';
        return html;
    },

    enumHtml: function () {
        var values = this.ValueRule.values;
        var html = '';
        html += '<div class="col-lg-10">';
        for (var i = 0; i < values.length; i++) {
            html += '<label class="editor-checkbox">';
            html += '<input type="checkbox" class="checkbox-editor" checked disabled >';
            html += '<span class="editor-checkbox-text">' + values[i].name + '</span>';
            html += '</label>';
        }
        html += '</div>';
        return html;
    },

    listHtml: function () {
        var values = this.ValueRule.values;
        var html = '';
        html += '<div class="product-mod col-lg-10">';
        if (values) {
            for (var i = 0; i < values.length; i++) {
                var value = values[i];
                if (value.url != undefined) {
                    html += '<div class="tag-img-item">';
                    html += '<span class="tag-img">';
                    html += '<img src="' + value.url + '">';
                    html += '</span>';
                    html += '<div class="tag-img-desc">' + value.name + '</div>';
                    html += '<div class="del-icon" onclick=\'removePartsPropertyListEle("' + this.TempId + '","' + value.id + '", this);\'></div>';
                    html += '</div>';
                } else {
                    html += '<div class="tag-item">';
                    html += '<span>' + value.name + '</span><i onclick=\'removePartsPropertyListEle("' + this.TempId + '","' + value.id + '", this, true);\'></i>';
                    html += '</div>';
                }

                //html += '<a name="list-delete" value-id="' + value.id + '">删除</a>';
            }
        }
        html += '</div>';
        return html;
    },

    tableHtml: function () {
        var values = this.ValueRule.values;
        var html = '';
        html += '<div class="product-mod col-lg-10">';
        if (values) {
            for (var i = 0; i < values.length; i++) {
                var value = values[i];
                if (value.url != undefined) {
                    html += '<div class="tag-img-item">';
                    html += '<span class="tag-img">';
                    html += '<img src="' + value.url + '">';
                    html += '</span>';
                    html += '<div class="tag-img-desc">' + value.name + '</div>';
                    html += '<div class="del-icon" onclick=\'removePartsPropertyListEle("' + this.TempId + '","' + value.id + '", this);\'></div>';
                    html += '</div>';
                } else {
                    html += '<div class="tag-item">';
                    html += '<span>' + value.name + '</span><i onclick=\'removePartsPropertyListEle("' + this.TempId + '","' + value.id + '", this, true);\'></i>';
                    html += '</div>';
                }

                //html += '<a name="list-delete" value-id="' + value.id + '">删除</a>';
            }
        }
        html += '</div>';
        return html;
    },

    fileHtml: function () {
        return '<label class="control-label col-lg-4">文件上传</label>';
    },

    autoHtml: function () {
        return '<label class="control-label col-lg-4">通过计算得出</label>';
    }

};


//设置属性值
function BomPartsProperty(ele, property_type, value_rule) {
    this.Ele = ele;
    this.PropertyType = property_type;
    this.ValueRule = value_rule;
    this.PropertyId = arguments[3] ? arguments[3] : false;

    this.Property = null;
}

BomPartsProperty.prototype = {
    render: function () {
        switch (this.PropertyType) {
            case "string":
                this.Property = new PropertyString(this.Ele, this.ValueRule);
                break;
            case "number":
                this.Property = new PropertyNumber(this.Ele, this.ValueRule);
                break;
            case "enum":
                this.Property = new PropertyEnum(this.Ele, this.ValueRule);
                break;
            case "list":
                this.Property = new PropertyList(this.Ele, this.ValueRule, this.PropertyId);
                break;
            case "table":
                this.Property = new PropertyTable(this.Ele, this.ValueRule, this.PropertyId);
                break;
            case "file":
                this.Property = new PropertyFile(this.Ele, this.ValueRule);
                break;
            case "auto":
                this.Property = new PropertyAuto(this.Ele, this.ValueRule);
                break;
        }
        this.Property.render();
    },
    saveValue: function () {
        if (this.Property) {
            this.ValueRule = this.Property.saveValue();
            if (this.ValueRule) {
                return true;
            }
            return false;
        }
    },
    getValue: function () {
        if (this.ValueRule) {
            return this.ValueRule;
        }
    }
};


function PropertyString(element, value_rule) {
    this.Element = element;
    this.ValueRule = value_rule;
}

PropertyString.prototype = {
    render: function () {
        this.Element.empty();
        var html = '';
        html += '<div class="form-horizontal" id="property_string_form">';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="min-length">最小长度：</label>';
        html += '<div class="col-lg-7"><input type="number" id="min-length" name="min-length" class="form-control" value=""></div>';
        html += '<label class="input-checkbox col-lg-3 label-inline"><input type="checkbox" class="label--input-inline" id="no-set-min"><span>不设置最小值</span></label>';
        html += '</div>';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="max-length">最大长度：</label>';
        html += '<div class="col-lg-7"><input type="number" id="max-length" name="max-length" class="form-control" value=""></div>';
        html += '<label class="input-checkbox col-lg-3 label-inline"><input type="checkbox" class="label--input-inline" id="no-set-max"><span>不设置最大值</span></label>';
        html += '</div>';

        html += '</div>';

        var obj = $(html);
        this.Element.append(obj);

        var that = this;
        //初始化数据
        if (this.ValueRule) {
            if (this.ValueRule.min_length.toString() != "") {
                obj.find("#min-length").val(this.ValueRule.min_length);
            } else {
                obj.find("#min-length").val("");
                obj.find("#min-length").attr("disabled", true);
                obj.find("#no-set-min").prop("checked", true);
            }

            if (this.ValueRule.max_length.toString() != "") {
                obj.find("#max-length").val(this.ValueRule.max_length);
            } else {
                obj.find("#max-length").val("");
                obj.find("#max-length").attr("disabled", true);
                obj.find("#no-set-max").prop("checked", true);
            }
        }

        //绑定事件
        obj.find("#no-set-min").click(function () {
            if ($(this).is(':checked')) {
                obj.find("#min-length").val("");
                obj.find("#min-length").attr("disabled", true);
            } else {
                obj.find("#min-length").attr("disabled", false);
            }
        });

        obj.find("#no-set-max").click(function () {
            if ($(this).is(':checked')) {
                obj.find("#max-length").val("");
                obj.find("#max-length").attr("disabled", true);
            } else {
                obj.find("#max-length").attr("disabled", false);
            }
        });
    },
    saveValue: function () {
        if (!this.ValueRule) {
            this.ValueRule = {};
        }
        var obj = this.Element;
        var min = obj.find("#min-length").val();
        var max = obj.find("#max-length").val();
        if (min != "") {
            min = parseInt(min);
        }
        if (max != "") {
            max = parseInt(max);
        }

        if (min != "" && max != "") {
            if (min >= max) {
                warningDialog("最小值不能大于等于最大值");
                return false;
            }
        }

        this.ValueRule.type = "string";
        this.ValueRule.min_length = min;
        this.ValueRule.max_length = max;

        return this.ValueRule;
    },
    getValue: function () {
        return this.ValueRule;
    }
};


function PropertyNumber(element, value_rule) {
    this.Element = element;
    this.ValueRule = value_rule;
}

PropertyNumber.prototype = {
    render: function () {
        this.Element.empty();
        var html = "";
        html += '<div class="form-horizontal" id="property_number_form">';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="min">最小值：</label>';
        html += '<div class="col-lg-7"><input type="number" id="min" name="min" class="form-control" value=""></div>';
        html += '<label class="input-checkbox col-lg-3 label-inline"><input type="checkbox" class="label--input-inline" id="no-set-min"><span>不设置最小值</span></label>';
        html += '</div>';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="max">最大值：</label>';
        html += '<div class="col-lg-7"><input type="number" id="max" name="max" class="form-control" value=""></div>';
        html += '<label class="input-checkbox col-lg-3 label-inline"><input type="checkbox" class="label--input-inline" id="no-set-max"><span>不设置最大值</span></label>';
        html += '</div>';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="step">变化步长：</label>';
        html += '<div class="col-lg-7"><input type="number" id="step" name="step" class="form-control" value=""></div>';
        html += '<label class="input-checkbox col-lg-3 label-inline"><input type="checkbox" class="label--input-inline" id="no-set-step"><span>不设置变化步长</span></label>';
        html += '</div>';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="step">是否整型：</label>';
        html += '<label class="input-checkbox col-lg-3 label-inline"><input type="checkbox" class="label--input-inline" id="is-integer"><span>是否整型</span></label>';
        html += '</div>';

        html += '</div>';

        var obj = $(html);
        this.Element.append(obj);

        var that = this;
        //初始化数据
        if (this.ValueRule) {
            if (this.ValueRule.min.toString() != "") {
                obj.find("#min").val(this.ValueRule.min);
            } else {
                obj.find("#min").val("");
                obj.find("#min").attr("disabled", true);
                obj.find("#no-set-min").prop("checked", true);
            }

            if (this.ValueRule.max.toString() != "") {
                obj.find("#max").val(this.ValueRule.max);
            } else {
                obj.find("#max").val("");
                obj.find("#max").attr("disabled", true);
                obj.find("#no-set-max").prop("checked", true);
            }

            if (this.ValueRule.step.toString() != "") {
                obj.find("#step").val(this.ValueRule.step);
            } else {
                obj.find("#step").val("");
                obj.find("#step").attr("disabled", true);
                obj.find("#no-set-step").prop("checked", true);
            }

            if (this.ValueRule.is_integer) {
                obj.find("#is-integer").prop("checked", true);
            } else {
                obj.find("#is-integer").prop("checked", false);
            }
        }

        //初始化checkbox事件
        obj.find("#no-set-min").click(function () {
            if ($(this).is(':checked')) {
                obj.find("#min").val("");
                obj.find("#min").attr("disabled", true);
                that.ValueRule.min = "";
            } else {
                obj.find("#min").attr("disabled", false);
            }
        });

        obj.find("#no-set-max").click(function () {
            if ($(this).is(':checked')) {
                obj.find("#max").val("");
                obj.find("#max").attr("disabled", true);
                that.ValueRule.max = "";
            } else {
                obj.find("#max").attr("disabled", false);
            }
        });

        obj.find("#no-set-step").click(function () {
            if ($(this).is(':checked')) {
                obj.find("#step").val("");
                obj.find("#step").attr("disabled", true);
                that.ValueRule.step = "";
            } else {
                obj.find("#step").attr("disabled", false);
            }
        });

        obj.find("#is-integer").click(function () {
            if ($(this).is(':checked')) {
                that.ValueRule.is_integer = 1;
            } else {
                that.ValueRule.is_integer = 0;
            }
        });
    },
    saveValue: function () {
        if (!this.ValueRule) {
            this.ValueRule = {};
        }
        var obj = this.Element;
        var min = obj.find("#min").val();
        var max = obj.find("#max").val();
        var step = obj.find("#step").val();

        if (min != "" && max != "") {
            min = parseInt(min);
            max = parseInt(max);
            if (min >= max) {
                warningDialog("最小值不能大于等于最大值");
                return false;
            }
        }

        this.ValueRule.type = "number";
        this.ValueRule.min = min;
        this.ValueRule.max = max;
        this.ValueRule.step = step;

        if (obj.find("#is-integer").is(":checked")) {
            this.ValueRule.is_integer = 1;
        } else {
            this.ValueRule.is_integer = 0;
        }

        return this.ValueRule;
    },
    getValue: function () {
        return this.ValueRule;
    }
};

function PropertyEnum(element, value_rule) {
    this.Element = element;
    this.ValueRule = value_rule;
}

PropertyEnum.prototype = {
    render: function () {
        this.Element.empty();
        var html = "";
        html += '<a type="button" class="btn btn-primary add-icon" id="proper-enum-add-btn"><div class="btn-icon"></div><div class="btn-text">添加</div></a>';
        html += '<table class="table table-bordered table-hover" id="property-rule-enum-table">';
        html += '<thead><tr><th>枚举编码</th><th>枚举名称</th><th>图片地址</th><th>操作</th></tr></thead>';
        html += '<tbody></tbody>';
        html += '</table>';

        var obj = $(html);
        this.Element.append(obj);

        this.addBtnEvent(obj);
        var that = this;

        if (this.ValueRule) {
            //初始化枚举列表
            for (var i = 0; i < this.ValueRule.values.length; i++) {
                var value = this.ValueRule.values[i];
                var tr = '<tr>';
                tr += '<th>' + value.code + '</th>';
                tr += '<th>' + value.name + '</th>';

                var image_html = "";
                if (value.url) {
                    image_html = '<img style="width: 25px; height: 25px" src="' + value.url + '"/>';
                }

                tr += '<th>' + image_html + '</th>';
                tr += '<th><a class="btn btn-link" id="edit">编辑</a><a class="btn btn-link" id="delete">删除</a></th>';
                tr += '</tr>';

                var $tr = $(tr);

                $tr.find('#edit').click(function () {
                    if ($('#proper-enum-add-btn').attr('data-status') == 'edit') {
                        return false;
                    }
                    $('#proper-enum-add-btn').attr('data-status', 'edit');

                    //修改当前tr样式
                    var code = $(this).parent().prev().prev().prev().text();
                    var name = $(this).parent().prev().prev().text();
                    var name_th = '<input type="text" id="proper-enum-name" value="' + name + '">';
                    $(this).parent().prev().prev().empty().append($(name_th));

                    var image_url = $(this).parent().prev().find("img").attr("src");
                    if (!image_url) {
                        image_url = "";
                    }
                    var image_th = '<input type="text" id="proper-enum-url" value="' + image_url + '">';
                    $(this).parent().prev().empty().append($(image_th));

                    var operate = $('<a class="btn btn-link" id="save">保存</a><a class="btn btn-link" id="cancel">取消</a>');

                    $(operate[0]).click(function () {
                        var name = $(this).parent().prev().prev().find("#proper-enum-name").val();
                        if (name == "") {
                            warningDialog("名称不能为空");
                            return false;
                        }

                        var url = $(this).parent().prev().find("#proper-enum-url").val();
                        if (!url) {
                            url = "";
                        }

                        for (var i = 0; i < that.ValueRule.values.length; i++) {
                            var value = that.ValueRule.values[i];
                            if (value.code == code) {
                                value.name = name;
                                value.url = url;
                                that.render();
                                break;
                            }
                        }

                    });

                    $(operate[1]).click(function () {
                        $('#proper-enum-add-btn').attr('data-status', '');
                        that.render();
                    });

                    $(this).parent().empty().append(operate);

                });

                $tr.find('#delete').click(function () {
                    if ($('#proper-enum-add-btn').attr('data-status') == 'edit') {
                        return false;
                    }
                    var code = $(this).parent().prev().prev().prev().text();
                    var remove_dx = 0;
                    for (var dx = 0; dx < that.ValueRule.values.length; dx++) {
                        var value = that.ValueRule.values[dx];
                        if (value.code == code) {
                            remove_dx = dx + 1;
                            break;
                        }
                    }
                    if (remove_dx) {
                        that.ValueRule.values.remove(remove_dx - 1);
                    }

                    $(this).parent().parent().remove();
                });

                obj.find("tbody").append($tr);
            }
        }

    },
    addBtnEvent: function (enum_obj) {
        var obj = enum_obj;
        var this_property = this;
        this.Element.find("#proper-enum-add-btn").click(function () {
            //判断是否存在有编辑的行，无则添加
            if ($(this).attr('data-status') == 'edit') {
                return false;
            }

            var that = $(this);
            var tr = '<tr>' +
                '<th><input type="text" id="proper-enum-code"></th>' +
                '<th><input type="text" id="proper-enum-name"></th>' +
                '<th><input type="text" id="proper-enum-url"></th>' +
                '<th> <a class="btn btn-link" id="save">保存</a><a class="btn btn-link" id="cancel">取消</a></th></tr>>';
            var $tr = $(tr);
            $tr.find("#save").click(function () {
                var code = $(this).parent().prev().prev().prev().find('input').val();
                var name = $(this).parent().prev().prev().find('input').val();
                var url = $(this).parent().prev().find('input').val();
                if (code == '' || name == '') {
                    warningDialog('编码或名称不能为空');
                    return false;
                }
                //添加到数据集合
                //判断是否有重复项
                if (this_property.ValueRule && this_property.ValueRule.values) {
                    for (var i = 0; i < this_property.ValueRule.values.length; i++) {
                        var value = this_property.ValueRule.values[i];
                        if (value.code == code) {
                            warningDialog('编码重复');
                            return false;
                        }
                    }
                } else {
                    this_property.ValueRule = {};
                    this_property.ValueRule.values = new Array();
                }
                this_property.ValueRule.values.push({code: code, name: name, url: url});
                this_property.render();

                that.attr('data-status', '');
            });

            $tr.find("#cancel").click(function () {
                $(this).parent().parent().remove();
                that.attr('data-status', '');
            });

            obj.find("tbody").append($tr);
            $(this).attr('data-status', 'edit');
        });
    },
    saveValue: function () {
        if (!this.ValueRule) {
            this.ValueRule = {};
        }
        this.ValueRule.type = "enum";
        if (!this.ValueRule.values || this.ValueRule.values.length == 0) {
            warningDialog("请输入值");
            return false;
        }

        return this.ValueRule;
    },
    getValue: function () {
        return this.ValueRule;
    }
};


function PropertyList(element, value_rule, property_id) {
    this.Element = element;
    this.ValueRule = value_rule;
    this.PropertyId = property_id;
    this.IsFromTable = (arguments[3] || value_rule.is_from_table) ? true : '';
}

PropertyList.prototype = {
    render: function () {
        $("#is_new_add_a").remove();
        $("#add_new_detail_data").remove();
        $("#property_table").remove();

        this.Element.empty();

        var header_html = '';
        header_html += '<div class="attribute-lists" style="width: 866px" id="bom_property_lists">' +
            '<div class="tagsinput-add-container">' +
            '<span class="selected-property-list"></span>' +
            '<a class="attribute-list-del-all" id="property_list_clear">清空</a>' +
            '</div>' +
            '<div id="property_more" class="more">+更多</div>' +
            '</div>';
        header_html += '<div class="panel panel-frame panel-operat panel-dialog">';
        header_html += '<div class="panel-body">';
        header_html += '<div class="panel-content">';
        header_html += '<input type="text" style="margin-right: 5px" id="search_value" class="form-control search-box search-name" placeholder="名称或编码" value="">';
        header_html += '<a id="search_value_icon" type="button" class="btn btn-warning search-icon">';
        header_html += '<div class="btn-icon"></div>';
        header_html += '<div class="btn-text">搜索</div></a>';
        header_html += '</div></div></div>';
        var header_obj = $(header_html);
        this.Element.append(header_obj);


        var html = '';
        html += '<input type="hidden" id="property_table" value="PropertyList">';
        // html += '<div class="checkbox-switch-wrapper checkbox-switch-wrapper-editor">';
        // html += '<input class="checkbox-switch" type="checkbox" onclick=\'switchToTable(\"' + this.Element.selector + '\", ' + JSON.stringify(this.ValueRule) + ', \"' + this.PropertyId + '\", true);\'>';
        // html += '<div class="checkbox-switch-circle"></div></div>';
        html += '<a style="margin-bottom: 12px;" class="btn btn-primary" onclick=\'switchToTable(\"' + this.Element.selector + '\", ' + JSON.stringify(this.ValueRule) + ', \"' + this.PropertyId + '\");\'>列表/表格切换</a>';
        html += '<table class="table table-bordered table-hover" id="property-rule-list-table">';
        html += '<thead><tr>';
        html += '<th><input type="checkbox" class="check-box" id="checkAll"></th>';
        for (var j = 0; j < this.ValueRule.columns.length; j++) {
            var column = this.ValueRule.columns[j];
            if (column.name.toLowerCase() != "id") {
                html += '<th>' + column.name + '</th>';
            }
        }
        html += '</tr></thead>';
        html += '<tbody></tbody>';
        html += '</table>';
        var obj = $(html);
        var that = this;
        obj.find("#checkAll").click(function () {
            if ($(this).is(':checked')) {
                that.Element.find('table input[type=checkbox]').prop("checked", true);
            } else {
                that.Element.find('table input[type=checkbox]').prop("checked", false);
            }
            var selected_values = new Array;
            that.Element.find('input[name="list-checkbox"]:checked').each(function (i, v) {
                var value = [];
                for (var j = 0; j < that.ValueRule.columns.length; j++) {
                    var column_key = that.ValueRule.columns[j].key;
                    var column_value = $(v).attr('data-' + column_key);
                    value[column_key] = column_value;
                    var obj = {};
                    for (var key in value) {
                        if (value.hasOwnProperty(key)) {
                            obj[key] = value[key]
                        }
                    }
                    selected_values[i] = obj;
                }
            });
            that.initSelectedValues(that, selected_values);
        });

        this.Element.append(obj);
        //get bom product proerty
        var datas = new Array();
        var params = {};

        params.property_id = this.PropertyId;
        params.table_name = this.ValueRule.table_name;
        params.columns = this.ValueRule.columns;
        params.where = this.ValueRule.where;
        params.is_from_table = this.IsFromTable;
        if (this.IsFromTable) {
            var temp_values = this.ValueRule.values;
            params.columns_str = '';
            delete this.ValueRule.values;
            for (var i = 0; i < this.ValueRule.columns.length; i++) {
                params.columns_str += this.ValueRule.table_name + "." + this.ValueRule.columns[i].key;
                if (i != this.ValueRule.columns.length - 1) {
                    params.columns_str += ",";
                }
            }
        }

        requestJson('../product-bom/get-property-list-by-proid-and-table-name-handler', params, false, function (response) {
            if (response.status == 0) {
                if (response.data) {
                    datas = datas.concat(response.data);
                }
            }
        });

        //编辑产品初始化的时候需要有list值
        if (datas.length > 0) {
            this.searchValues(datas, this, '', temp_values);
            header_obj.find('#search_value_icon').click(function () {
                var search_value = header_obj.find('#search_value').val().trim();
                that.searchValues(datas, that, search_value, temp_values);
            });
        }
        if (this.ValueRule != undefined) addNewDetailData(this);


        setTimeout(function () {
            header_obj.find("#bom_property_lists").width(header_obj.find(".panel-body").width());
            $("#property_more").mouseenter(function () {
                $(this).parent().animate({height: $(this).parent().find('.tagsinput-add-container').height() + 9}, 200);
                var checked_length = that.Element.find('input[name="list-checkbox"]:checked').length;
                if ($(this).parent().height() == 40 && checked_length > 0) {
                    $('#paper_sizes_more').show();
                } else {
                    $(this).hide();
                }
            });
            $('#bom_property_lists').mouseleave(function () {
                $(this).animate({height: 40}, 200);
                var checked_length = that.Element.find('input[name="list-checkbox"]:checked').length;
                if (checked_length > 0) {
                    $('#property_more').show();

                }
            });

        }, 500);
        //清空
        header_obj.find("#property_list_clear").click(function () {
            that.Element.find('input[type=checkbox]').prop("checked", false);
            that.initSelectedValues(that, '');
        });
        if (this.ValueRule.values == undefined) that.initSelectedValues(that, '');
    },
    saveValue: function () {
        if (!this.ValueRule) {
            this.ValueRule = {};
        }
        this.ValueRule.type = "list";
        var elements = this.Element.find("input[name='list-checkbox']:checkbox:checked");
        if (elements.length == 0) {
            warningDialog('请选择列表项');
            return false;
        }
        if (this.ValueRule.values) {
            this.ValueRule.values.length = 0;
        } else {
            this.ValueRule.values = new Array();
        }
        for (var i = 0; i < elements.length; i++) {
            var element = $(elements[i]);
            var value = "";
            for (var j = 0; j < this.ValueRule.columns.length; j++) {
                var column = this.ValueRule.columns[j];
                value += "\"" + column["key"] + "\"" + ":" + "\"" + element.attr("data-" + column["key"]) + "\"";
                if (j < this.ValueRule.columns.length - 1) {
                    value += ",";
                }
            }
            if (element.attr("data-url") != undefined) {
                value += "," + "\"url\"" + ":" + "\"" + element.attr("data-url") + "\"";
            }
            this.ValueRule.values.push(JSON.parse("{" + value + "}"));
        }
        return this.ValueRule;
    },
    searchValues: function (datas, that, search_value, temp_values) {

        that.Element.find("tbody").empty();

        // if (that.ValueRule.values != undefined) {
        var selected_values = that.ValueRule.values.clone();
        // }
        var tmp_array = [];
        var other_data = [];
        if (selected_values.length > datas.length) {
            for (var v = 0; v < datas.length; v++) {
                tmp_array[datas[v].id] = true;
            }
            for (var k = 0; k < selected_values.length; k++) {
                if (!tmp_array[selected_values[k].id]) {
                    selected_values[k].is_deleted = true;
                    other_data.push(selected_values[k]);
                }
            }
            if (other_data.length > 0) {
                datas = datas.concat(other_data);
            }
        }

        that.initSelectedValues(that, selected_values);

        var is_add_url = false; //是否添加完图片操作td

        for (var i = 0; i < datas.length; i++) {
            var data = datas[i];
            if (data.name.indexOf(search_value) >= 0 || data.code.indexOf(search_value) >= 0) {
                var html_body = '';
                html_body += '<tr>';
                var check_str = "";
                if (this.IsFromTable) {
                    this.ValueRule.values = temp_values;
                }
                if (that.ValueRule.values) {
                    for (var j = 0; j < that.ValueRule.values.length; j++) {
                        if (that.ValueRule.values[j].id == datas[i].id) {
                            check_str = 'checked="checked"';
                            break;
                        }
                    }
                } else {
                    //刚初始化默认全部选中
                    //check_str = 'checked="checked"';
                }
                var chk = '';
                chk = '<td><input type="checkbox" class="check-box" ' + check_str + ' name="list-checkbox" ';
                for (var j = 0; j < that.ValueRule.columns.length; j++) {
                    var column = that.ValueRule.columns[j];
                    chk += ' data-' + column.key + '="' + data[column.key] + '"';
                }
                if (data['url'] != undefined) {
                    chk += ' data-url="' + data['url'] + '"';
                }
                chk += '></td>';
                var td = '';
                for (var j = 0; j < this.ValueRule.columns.length; j++) {
                    var column = this.ValueRule.columns[j];
                    if (column["key"].toLowerCase() != "id") {
                        td += '<td>' + data[column.key] + '</td>';
                    }
                }
                if (data.hasOwnProperty('url')) {
                    if (!is_add_url) {
                        this.Element.find("thead").find("tr").append($("<td>图片</td>"));
                        is_add_url = true;
                    }

                    if (data['url'] != undefined) {
                        td += '<td><img style="width: 25px;height: 25px" src="' + data['url'] + '"></td>';

                    } else {
                        td += '<td><img style="width: 25px;height: 25px" src="/images/no-img.jpg"></td>';
                    }
                }
                html_body += chk + td + '</tr>';
                this.Element.find("tbody").append($(html_body));
            }
        }
        this.checkAll(that);

        that.Element.find('input[name="list-checkbox"]').click(function () {
            that.checkAll(that);
            var selected_values = new Array;
            that.Element.find('input[name="list-checkbox"]:checked').each(function (i, v) {
                var value = [];
                for (var j = 0; j < that.ValueRule.columns.length; j++) {
                    var column_key = that.ValueRule.columns[j].key;
                    var column_value = $(v).attr('data-' + column_key);
                    value[column_key] = column_value;
                    var obj = {};
                    for (var key in value) {
                        if (value.hasOwnProperty(key)) {
                            obj[key] = value[key]
                        }
                    }
                    selected_values[i] = obj;
                }
            });
            that.initSelectedValues(that, selected_values);
        });
        if (that.ValueRule.values != undefined) {
            var selected_values = that.ValueRule.values.clone();
            that.initSelectedValues(that, selected_values);
        }
    },
    initSelectedValues: function (that, selected_values) {
        that.Element.find('.attribute-lists').find('.selected-property-list').empty();
        if (selected_values && selected_values.length > 0) {
            for (var i = 0; i < selected_values.length; i++) {
                var value = selected_values[i];
                var html = '<div id="propertyValueId' + value.id + '" class="tag-item"><span>' + value.name + '</span><i property-value-id="' + value.id + '"></i></div>';
                var obj = $(html);
                obj.find("i").click(function () {
                    var property_value_id = $(this).attr("property-value-id");

                    $(this).parent().remove();
                    that.Element.find('input[data-id="' + property_value_id + '"]').prop('checked', false);

                    that.Element.find("#checkAll").prop('checked', false);
                });
                that.Element.find('.attribute-lists').find('.selected-property-list').append(obj);
            }

            $("#property_list_clear").show();
            $("#property_more").show();
        } else {
            $("#property_list_clear").hide();
            $("#property_more").hide();
        }
        this.ValueRule.is_from_table = this.IsFromTable;
        return this.ValueRule;
    },
    getValue: function () {
        return this.ValueRule;
    },
    checkAll: function (that) {
        var checked_length = that.Element.find('input[name="list-checkbox"]:checked').length;
        var all_length = that.Element.find('input[name="list-checkbox"]').length;

        if (checked_length == all_length && all_length != 0) {
            that.Element.find("#checkAll").prop('checked', true);
        } else {
            that.Element.find("#checkAll").prop('checked', false);
        }
    }
};


function PropertyTable(element, value_rule) {
    this.Element = element;
    this.ValueRule = value_rule;
    this.PropertyId = arguments[2] ? arguments[2] : false;
}

PropertyTable.prototype = {
    render: function () {
        $("#is_new_add_a").remove();
        $("#add_new_detail_data").remove();
        $("#current_property_value").remove();
        $("#property_table").remove();
        this.Element.empty();

        var html = '';

        html += '<input type="hidden" id="property_table" value="PropertyTable">';
        // html += '<div class="checkbox-switch-wrapper checkbox-switch-wrapper-editor">';
        // html += '<input class="checkbox-switch" type="checkbox" onclick=\'switchToList(\"' + this.Element.selector + '\", ' + JSON.stringify(this.ValueRule) + ', \"' + this.PropertyId + '\", true);\'>';
        // html += '<div class="checkbox-switch-circle"></div></div>';
        html += '<a style="margin-bottom: 12px;" class="btn btn-primary" onclick=\'switchToList(\"' + this.Element.selector + '\", ' + JSON.stringify(this.ValueRule) + ', \"' + this.PropertyId + '\");\'>列表/表格切换</a>';
        html += '<table class="table table-bordered table-hover" id="property-rule-list-table">';
        html += '<thead><tr>';
        html += '<th><input type="checkbox" class="check-box" disabled id="checkAll"></th>';
        for (var j = 0; j < this.ValueRule.columns.length; j++) {
            var column = this.ValueRule.columns[j];
            if (column.name.toLowerCase() != "id") {
                html += '<th>' + column.name + '</th>';
            }
        }
        html += '</tr></thead>';
        html += '<tbody></tbody>';
        html += '</table>';
        var obj = $(html);

        var that = this;
        obj.find("#checkAll").click(function () {
            if ($(this).is(':checked')) {
                that.Element.find('input[type=checkbox]').prop("checked", true);
            } else {
                that.Element.find('input[type=checkbox]').prop("checked", false);
            }
        });

        this.Element.append(obj);


        //get bom product proerty
        var datas = new Array();
        var params = {};

        params.value_ids = new Array();
        params.table_name = this.ValueRule.table_name;
        params.columns = "";
        for (var i = 0; i < this.ValueRule.columns.length; i++) {
            params.columns += params.table_name + "." + this.ValueRule.columns[i].key;
            if (i != this.ValueRule.columns.length - 1) {
                params.columns += ",";
            }
        }
        params.where = this.ValueRule.where;

        requestJson('../product-bom/get-property-list-by-ids-and-talbe-name-handler', params, false, function (response) {
            if (response.status == 0) {
                if (response.data) {
                    datas = datas.concat(response.data);
                }
            }
        });


        //编辑产品初始化的时候需要有list值
        if (datas.length > 0) {
            var is_add_url = false; //是否添加完图片操作td
            for (var i = 0; i < datas.length; i++) {
                var data = datas[i];
                var html_body = '';
                html_body += '<tr>';
                //var check_str = "";
                var chk = '';
                var td = '';
                //if (this.ValueRule.values) {
                //    for (var j = 0; j < this.ValueRule.values.length; j++) {
                //        if (this.ValueRule.values[j].id == datas[i].id) {
                //            check_str = 'checked="checked"';
                //            break;
                //        }
                //    }
                //} else {
                //    //刚初始化默认全部选中
                //    check_str = 'checked="checked"';
                //}
                chk = '<td><input type="checkbox" class="check-box" disabled name="list-checkbox" ';
                for (var j = 0; j < this.ValueRule.columns.length; j++) {
                    var column = this.ValueRule.columns[j];
                    if (column["key"].toLowerCase() != "id") {
                        td += '<td>' + data[column.key] + '</td>';
                    }
                    //td += '<td>' + data[column.key] + '</td>';
                    chk += ' data-' + column.key + '="' + data[column.key] + '"';
                }
                if (data['url'] != undefined) {
                    chk += ' data-url="' + data['url'] + '"';
                }
                chk += '></td>';
                if (data.hasOwnProperty('url')) {
                    if (!is_add_url) {
                        this.Element.find("thead").find("tr").append($("<td>图片</td>"));
                        is_add_url = true;
                    }

                    if (data['url'] != undefined) {
                        td += '<td><img style="width: 25px;height: 25px" src="' + data['url'] + '"></td>';
                    } else {
                        td += '<td><img style="width: 25px;height: 25px" src="/images/no-img.jpg"></td>';
                    }
                }
                html_body += chk + td + '</tr>';

                this.Element.find("tbody").append($(html_body));
            }
            //region liutao
            var category_id = '';
            if (this.ValueRule.where.length > 0) {

                var where_str = this.ValueRule.where.toLowerCase();
                if (where_str.indexOf('and') >= 0) {
                    var wheres = where_str.split('and');
                    for (var k = 0; k < wheres.length; k++) {
                        if (wheres[k].indexOf('data_category_id') >= 0) {
                            category_id = wheres[k].split('=')[1].trim();
                            break;
                        }
                    }
                } else {
                    var wheres = where_str.split('=');
                    for (var m = 0; m < wheres.length; m++) {
                        if (wheres[m].indexOf('data_category_id') >= 0) {
                            category_id = wheres[m + 1].trim();
                            break;
                        }
                    }

                }

            }
            //endregion liutao end
        }
        //region liutao
        if (this.ValueRule != undefined) {
            var actions = this.ValueRule.table_name.trim().split('_');
            actions.splice(0, 2);
            var action = '';
            for (var k = 0; k < actions.length; k++) {
                action += '-' + actions[k];
            }
            action = action.substr(1);
            if (action == "data" || action == "post-printing-data") {
                action = 'base-' + action;
            }
            var html_a = '';
            html_a += '<a id="is_new_add_a" class="btn btn-primary putaway-icon" style="float: left"';
            if (category_id != '') {
                html_a += 'onclick=onNewModalEdit(\"' + action + '\",' + category_id + ')>';
            } else {
                html_a += 'onclick=onNewModalEdit(\"' + action + '\")>';
            }
            html_a += '<div class="btn-icon"></div><div class="btn-text">添加更多</div></a>';
            $(html_a).prependTo("#partsPropertyValueModalFooter");
        }
        //endregion liutao end
    },
    saveValue: function () {
        if (!this.ValueRule) {
            this.ValueRule = {};
        }
        this.ValueRule.type = "table";
        var elements = this.Element.find("input[name='list-checkbox']:checkbox");
        if (this.ValueRule.values) {
            this.ValueRule.values.length = 0;
        } else {
            this.ValueRule.values = new Array();
        }
        for (var i = 0; i < elements.length; i++) {
            var element = $(elements[i]);
            var value = "";
            for (var j = 0; j < this.ValueRule.columns.length; j++) {
                var column = this.ValueRule.columns[j];
                value += "\"" + column["key"] + "\"" + ":" + "\"" + element.attr("data-" + column["key"]) + "\"";
                if (j < this.ValueRule.columns.length - 1) {
                    value += ",";
                }
            }
            if (element.attr("data-url") != undefined) {
                value += "," + "\"url\"" + ":" + "\"" + element.attr("data-url") + "\"";
            }

            this.ValueRule.values.push(JSON.parse("{" + value + "}"));
        }
        return this.ValueRule;
    },
    getValue: function () {
        return this.ValueRule;
    }
};


function PropertyFile(element, value_rule) {
    this.Element = element;
    this.ValueRule = value_rule;
}

PropertyFile.prototype = {
    render: function () {
        this.Element.empty();

        var html = "";
        html += '<div class="form-horizontal" id="property_number_form">';

        html += '<div class="form-group"><label class="control-label col-lg-2">说明：</label>';
        html += '<div class="col-lg-8">文件上传</div>';
        html += '</div>';

        html += '</div>';
        var obj = $(html);

        this.Element.append(obj);
    },
    saveValue: function () {
        if (this.ValueRule) {
            this.ValueRule = {};
        }
        this.ValueRule.type = "file";
        return this.ValueRule;
    },
    getValue: function () {

        return this.ValueRule;
    }
};


function PropertyAuto(element, value_rule) {
    this.Element = element;
    this.ValueRule = value_rule;
}

PropertyAuto.prototype = {
    render: function () {
        this.Element.empty();

        var html = "";
        html += '<div class="form-horizontal" id="property_number_form">';

        html += '<div class="form-group"><label class="control-label col-lg-2">说明：</label>';
        html += '<div class="col-lg-8">使用公式进行计算</div>';
        html += '</div>';

        html += '</div>';
        var obj = $(html);

        this.Element.append(obj);
    },
    saveValue: function () {
        if (this.ValueRule) {
            this.ValueRule = {};
        }

        this.ValueRule.type = "auto";
        return true;
    },
    getValue: function () {

        return this.ValueRule;
    }
};

function addNewDetailData(obj) { //产品 部件 list
    var html_a = '';
    html_a += '<a id="add_new_detail_data" class="btn btn-primary putaway-icon" style="float: left" onclick=showListModel(\"' + obj.ValueRule.property_id + '\","parts")>';
    html_a += '<div class="btn-icon"></div><div class="btn-text">展示详情</div></a>';
    $(html_a).prependTo("#partsPropertyValueModalFooter");
}

